import ajax from "./promiseAjax.js";

let myBook = document.querySelector(".myBook");
let startBook = document.querySelector(".startBook");
let userName = document.querySelector(".userName>input");
let phone = document.querySelector(".phone>input");
let idCard = document.querySelector(".idCard>input");

let registerApi = "http://newshopapi.0melon0.cn/api/f_user/register";

//我的预约
myBook.addEventListener("click", () => {
  // console.log(sessionStorage.getItem("completeTime"));
  if (sessionStorage.getItem("completeTime") == null) {
    location.href = 'noRecord.html';
  } else {
    location.href = 'bookingRecord.html';
  }
})

//开始预约
startBook.addEventListener("click", () => {
  let data = {
    "name": `${userName.value}`,
    "phone": `${phone.value}`,
    "card": `${idCard.value}`,
  }

  ajax(registerApi, data, "POST").then(res => {
    console.log(res.result.token);

    //存储token
    sessionStorage.setItem("token", res.result.token);
    sessionStorage.setItem("phone", phone.value);
    location.href = "appointment.html";
  }).catch(err => {
    console.log(err);
    if (err.msg) {
      alert("输入有误。请重新输入！")
    }
  })
})

